package at.ac.univie.zsu.aguataplan.main;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import at.ac.univie.zsu.aguataplan.property.GeneralProperties;
import at.ac.univie.zsu.aguataplan.ui.IForegroundChange;
import at.ac.univie.zsu.aguataplan.ui.UIConstants;
import at.ac.univie.zsu.aguataplan.ui.VideoPlayerPanel;
import at.ac.univie.zsu.aguataplan.util.EnvironmentHandler;
import at.ac.univie.zsu.aguataplan.util.InitLogger;

/**
 * used to adapt player without starting aguataplan
 * 
 * @author gerry
 * 
 */
@Configurable
public class MainPlayer implements UIConstants {
	private static final long serialVersionUID = 1L;

	public static final String s = "C:\\Users\\bw\\Documents\\test.avi";

	@Autowired
	private GeneralProperties generalProperties;

	private JFrame f;
	private VideoPlayerPanel videoPlayerPanel;

	// ----------------------------------------------------
	// MAIN
	// ----------------------------------------------------

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Video Player tester...");

		InitLogger.initLogger();
		EnvironmentHandler.readArgs(args);

		new ClassPathXmlApplicationContext("META-INF/spring/applicationContextPlayer.xml");

	}

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	@SuppressWarnings("unused")
	private void init() {
		f = new JFrame();
		f.setSize(1000, 700);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				videoPlayerPanel.quit();
				System.exit(0);
			}
		});
		setLookAndFeel();

		// ----------------

		videoPlayerPanel = new VideoPlayerPanel(generalProperties, new IForegroundChange() {

			@Override
			public void setInForeground(boolean value) {
			}

		}, s);

		videoPlayerPanel.setSize(PANEL_VIDEO_X, PANEL_VIDEO_Y);
		videoPlayerPanel.setLocation(SPACING, SPACING);
		f.add(videoPlayerPanel);

		videoPlayerPanel.init();
	}

	private void setLookAndFeel() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
		} catch (InstantiationException e) {
		} catch (IllegalAccessException e) {
		} catch (UnsupportedLookAndFeelException e) {
		}
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public GeneralProperties getGeneralProperties() {
		return generalProperties;
	}

	public void setGeneralProperties(GeneralProperties generalProperties) {
		this.generalProperties = generalProperties;
	}

}
